package com.yb.xczx.cloud.media.controller;


import com.yb.core.exception.XczxBusinessException;
import com.yb.xczx.cloud.media.model.dto.UploadMediaFilesDTO;
import com.yb.xczx.cloud.media.model.vo.UploadFileResultVo;
import com.yb.xczx.cloud.media.service.IMediaFilesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;

import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

/**
 * <p>
 * 媒资信息 前端控制器
 * </p>
 *
 * @author 姚兵
 * @since 2023-05-09
 */
@RestController
@Api("媒资文件管理")
public class MediaFilesController {

    @Autowired
    IMediaFilesService mediaFilesService;

    @ApiOperation("上传图片")
    @PostMapping("upload/coursefile")
    public UploadFileResultVo upload(@RequestPart("filedata")MultipartFile filedata){
        Long companyId=123L;
        UploadMediaFilesDTO mediaFilesDTO = new UploadMediaFilesDTO();
        mediaFilesDTO.setFileSize(filedata.getSize());
        mediaFilesDTO.setFilename(filedata.getOriginalFilename());
        File tempFile = null;
        try {
            tempFile = File.createTempFile("minio", ".tmp");
            filedata.transferTo(tempFile);
            mediaFilesDTO.setTempFilePath(tempFile.getAbsolutePath());
        } catch (IOException e) {
            throw new XczxBusinessException(e);
        }

        return mediaFilesService.upload(companyId,mediaFilesDTO);
    }
}
